
let mDebug = true

let Network =
{
    httpRequest(url, data, headers, callback, method, type){

        let http = new XMLHttpRequest()

        if (type)
        {
            http.responseType = type || 'text'
        }
        
        http.open(method || 'POST', url, true)

        if (headers)
        {
            for (let k in headers)
            {
                http.setRequestHeader(k, headers[k])
            }
        }

        http.timeout = 5000
        
        http.setRequestHeader("X-Requested-With", "XMLHttpRequest")
        http.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8")

        //if (cc.sys.isNative) 
        //{
        //    http.setRequestHeader("Accept-Encoding", "gzip,deflate")
        //}

        http.onerror = () =>{

                if (http)
                {
                    Log.e("Network, http send error <", url, ">, ", 'status-' + http.status)
                    
                    http = null

                    callback && callback()
                }
            }

        http.ontimeout = ()=>{

                if (http)
                {
                    Log.e("Network, http send time out <", url, ">: ", 'status-' + http.status)

                    http = null

                    callback && callback()
                }
            }

        http.onreadystatechange = () => {

                if (http && http.readyState == 4)
                {
                    let code = http.status

                    if (code && code >= 200 && code < 207)
                    {
                        if (mDebug)
                        {
                            Log.d("Network, response<", url, ">: ", http.response)
                        }

                        callback && callback(http.response)
                    }
                    else
                    {
                        Log.d("Network, fail response<", url, ">: status-", code)

                        callback && callback()
                    }

                    http = null
                }                
            }

        if (mDebug)
        {
            Log.d("Network, http send<", url, ">: ", data || '')
        }

        http.send(data)
    },

    download(url, path, params, callback){

        let http = new XMLHttpRequest()

        let requestURL = url + (path ? path : '')
        if (params) 
        {
            requestURL = requestURL + "?" + params
        }

        http.timeout = 3000
        http.responseType = "arraybuffer"

        http.open("GET", requestURL, true)

        if (cc.sys.isNative)
        {
            http.setRequestHeader("Accept", "text/html")
            http.setRequestHeader("Accept-Charset", "utf-8")
            http.setRequestHeader("Accept-Encoding", "gzip,deflate")
        }

        http.onreadystatechange = () =>
        {
            if (http.readyState === 4)
            {
                if (http.status >= 200 && http.status < 300)
                {
                    let buffer = http.response

                    let d = new DataView(buffer)
                    let r = new Uint8Array(buffer.byteLength)

                    let len = r.length

                    for (let i = 0; i < len; ++i) 
                    {
                        r[i] = d.getUint8(i)
                    }
                    callback(r)
                }
                else
                {
                    callback(null)
                }
                http = null
            }
        }

        http.send()

        return http
    },

    downloadText(url, tryTimes, callback, ingoreTime){

        let http2 = new XMLHttpRequest()
        
        if (!ingoreTime)
        {
           url += '?t=' + cc.sys.now()
        }
        
        http2.open("GET", url, true)
    
        if (http2.overrideMimeType) 
        {
            http2.overrideMimeType('text\/plain; charset=utf-8')
        }
        
        http2.timeout = 10000
        //http2.setRequestHeader("Cache-Control","no-store")

        http2.onreadystatechange = () =>
        {
            if (http2.readyState === 4)
            {
                if (http2.status >= 200 && http2.status < 300)
                {
                    callback(http2.responseText)
                }
                else
                {
                    Log.e("downloadText file fail: " + url)
                    --tryTimes
        
                    if (tryTimes > 0)
                    {
                        Network.downloadText(url, tryTimes, callback, true)
                    }
                    else
                    {
                        callback()
                    }
                }

                http2 = null
            }
        }
    
        http2.send()
    }
}

module.exports = Network
